/*
 * estructuras.h
 *
 *  Created on: 19/11/2011
 *      Author: utn_so
 */

#ifndef ESTRUCTURAS_H_
#define ESTRUCTURAS_H_
#include "libreriasExternas.h"


typedef struct{
	char * tabla;
	sem_t semaforo;
	int32_t contador;
	int32_t modificado;
}t_fat;

typedef struct{
	char * tabla;
	sem_t semaforo;
	int32_t contador;
}t_fdt;

typedef struct{
	bool primerBit :1;
	bool segundoBit :1;
	bool tercerBit :1;
	bool cuartoBit :1;
	bool quintoBit :1;
	bool sextoBit :1;
	bool septimoBit :1;
	bool octaboBit :1;
}t_byte;

typedef struct{
	t_byte primer;
	t_byte segundo;
	t_byte tercer;
	t_byte cuarto;
}t_int;

typedef struct{
	bool PrimerBit :1;
	bool SegundoBit :1;
	bool TercerBit :1;
	bool CuartoBit :1;
	bool QuintoBit :1;
	bool SextoBit :1;
	bool SeptimoBit :1;
	bool OctavoBit :1;
	bool NovenoBit:1;
	bool DecimoBit :1;
	bool DecimoPrimerBit :1;
	bool DecimoSegundoBit :1;
	bool DecimoTercerBit :1;
	bool DecimoCuartoBit :1;
	bool DecimoQuintoBit :1;
	bool DecimoSextoBit :1;
}t_time_fat;

typedef struct{
	t_time_fat dosBytes;
}t_date_fat;

typedef struct{
	t_byte byte;
}t_mili_fat;


typedef struct{
	t_mili_fat miliFat;
	t_time_fat timeFat;
	t_date_fat dateFat;
}t_createDate_fat;









typedef struct {
int puerto;
char *ip;
int maxConections;
int tamCache;
t_log *log;
int32_t blockSize;
}t_config;




typedef struct{
/*
char jumpInstruction[3]; //No se usa
char OEMName[8]; //No se usa
uint16_t bytesPerSector;
char sectorsPerCluster;
uint16_t reservedSectorCount;
char numberOfFATS;
uint16_t numberOfRootDirectoriesEntries; //No se usa
uint16_t totalSectors;
char mediaDescriptor;
uint16_t sectorsPerFat;
uint16_t sectorsPerTrack;
uint16_t numberOfHeads;//No se usa
int hiddenSectorsBeforeFAT;
int totalSector;
*/
	char jumpInstruction[3]; //No se usa
	char OEMName[8]; //No se usa
	uint16_t bytesPerSector;
	char sectorsPerCluster;
	uint16_t reservedSectorCount;
	char numberOfFATS;
	uint16_t numberOfRootDirectoriesEntries; //No se usa
	uint16_t totalSectors;
	char mediaDescriptor;
	uint16_t sectorsPerFat;
	uint16_t sectorsPerTrack;
	uint16_t numberOfHeads;//No se usa
	uint32_t hiddenSectorsBeforeFAT;
	uint32_t totalSectorsExt;


}t_bootSector;

typedef struct{
	    uint32_t sectorsFAT;
		uint16_t fatFlags;
		uint16_t version;
		int32_t clusterRootDirectory;
		uint16_t fsInfoSector;
		uint16_t sectorCopyBootSector;
		char reserved[12];
		char physicalDriveNumber;
		char reserved2;
		char extendedbootSignature;
		uint32_t idSerialNumber;
		char volumeLabel[12];
		char fileSystemType[12];
	//Operating boot signature 420
	//Boot sector signature 0x55,0xAA 2
}t_extend_bios_parameter_block;


typedef struct{

	t_bootSector bootSector;
	t_extend_bios_parameter_block extendBios;
	t_fat * fat;
	int fatSizeBytes;
	char * fdt;
	int fdtSizeInSectors;

}t_fat32;



typedef struct{
	char DOSFilename[8];
	char fileExtention[3];
	char fileAtributes;
	char reserved;
	char createTimeFineResolution;
	uint16_t createTime;
	uint16_t createDate;
	uint16_t lastAccessDate;
	uint16_t EA_Index;
	uint16_t lastModifiedTime;
	uint16_t lastModifiedDate;
	uint16_t startCluster;
	uint32_t fileSizeBytes;
}t_fdtEntry;

/*
typedef struct{
	char sequenceNumber;
	char longName1[10];
	char fileAtributes;
	char reserved;
	char checksum;
	char longName2[12];
	uint16_t startCluster;
	char longName3[4];
}t_fdtEntryLong;
*/

typedef struct{
	char secuenceNumber;
	char nameCharacters1[10];
	char fileAtributes;
	char reserved;
	char checkSum;
	char nameCharacters2[12];
	uint16_t startCluster;
	char nameCharacters3[4];
}t_fdtLongEntry;


typedef struct{
	t_fdtLongEntry entradaLarga;
	t_fdtEntry entradaCorta ;
	int32_t tieneEntradaLarga;
	uint32_t clusterContenido;
	uint32_t posContenido;
	uint32_t clusterPadre;
}t_fdtCompleteEntry;


typedef struct{
	bool readOnly :1;
	bool hidden :1;
	bool system :1;
	bool volumeLabel :1;
	bool subdirectory :1;
	bool archive :1;
	bool debice :1;
}t_fat32_attributes;





typedef struct{
	char name[13];
	t_fdtEntry primerRegistro;
	t_fdtEntry segundoRegistro;
	t_list hijos;
	t_list listaClusters;

}t_nodoFatData;


typedef struct{
	t_nodoFatData *nodoPadre;
	t_nodoFatData data;
}t_nodoFat;

typedef struct{
	int numeroCluster;
	t_list * clusters;
	t_list * bloques;
	char * data;
	int contenidoEnFAT;
}t_cluster;

typedef struct{
	t_list * sectores;
	int32_t nBloque;
	int32_t nBloqueT;
	int32_t nCluster;
	char * data;
}t_bloque;

typedef struct{
	int numeroSector;
	char * data;
//	char * contenido;
}t_sector;

typedef struct{
	char * data;
	uint32_t nBloque;
	int32_t modificado;
}t_cache_bloqueDeArchivo;

typedef struct{
	t_list * listaBloques;
	int modificado;
}t_cache_clusterDeArchivo;


typedef struct{
	char * pathYNombreDelArhivo;
	t_fdtCompleteEntry * entradaDelArchivo;
	//t_list * listaClusters;
	sem_t semaforo;
	uint32_t referencias;
	t_list * listaBloquesCache;
	uint32_t clusterPadre;
	uint32_t clusterContenido;
	uint32_t posicionByteEnClusterPadre;
}t_cache_archivo;


typedef struct{
	t_list * listaArchivos;
	sem_t semaforo;
}t_cache;

typedef struct{
	char * pathYNombreDelArhivo;
	t_fdtCompleteEntry * entradaDelArchivo;
	//t_list * listaClusters;
	sem_t semaforo;
	uint32_t referencias;
	t_list * listaBloquesCache;
	uint32_t clusterPadre;
	uint32_t clusterContenido;
	uint32_t posicionByteEnClusterPadre;
	t_list * listaClusters;
}t_cache_archivo_root;

typedef struct {
	t_cache archivos;
	t_cache_archivo_root root;
	sem_t semaforo;
	t_fat * fat;
}t_cachePfs;

typedef struct {
	t_config configArch;
	t_fat32 fat32;
	t_cachePfs cacheArchivos;
}t_pfs;


#endif /* ESTRUCTURAS_H_ */
